home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / emulator / bsvc-1.000 / bsvc-1 / bsvc-1.0.4 / src / Assemblers / 68kasm / object.c < prev    next >
C/C++ Source or Header  |  1995-07-26  |  5KB  |  192 lines

  1. /******************************************************************************
  2.  * $Id: object.c,v 1.1 1994/08/30 00:04:14 bmott Exp $
  3.  ******************************************************************************
  4.  *
  5.  *        OBJECT.C
  6.  *        Object File Routines for 68000 Assembler
  7.  *
  8.  *    Function: initObj()
  9.  *        Opens the specified object code file for writing. If
  10.  *        the file cannot be opened, then the routine prints a
  11.  *        message and exits. 
  12.  *
  13.  *        outputObj()
  14.  *        Places the data whose size, value, and address are
  15.  *        specified in the object code file. If the new data
  16.  *        would cause the current S-record to exceed a certain
  17.  *        length, or if the address of the current item doesn't
  18.  *        follow immediately after the address of the previous
  19.  *        item, then the current S-record is written to the file
  20.  *        (using writeObj) and a new S-record is started,
  21.  *        beginning with the specified data. 
  22.  *
  23.  *        writeObj()
  24.  *        Writes the current S-record to the object code file.
  25.  *        The record length and checksum fields are filled in
  26.  *        before the S-record is written. If an error occurs
  27.  *        during the writing, the routine prints a message and
  28.  *        exits. 
  29.  *
  30.  *        finishObj()
  31.  *        Flushes the S-record buffer by writing out the data in
  32.  *        it (using writeObj), if any, then writes a termination
  33.  *        S-record and closes the object code file. If an error 
  34.  *        occurs during this write, the routine prints a messge 
  35.  *        and exits.
  36.  *
  37.  *     Usage: initObj(name)
  38.  *        char *name;
  39.  *
  40.  *        outputObj(newAddr, data, size)
  41.  *        int data, size;
  42.  *
  43.  *        writeObj()
  44.  *
  45.  *        finishObj()
  46.  *        
  47.  *      Author: Paul McKee
  48.  *        ECE492    North Carolina State University
  49.  *
  50.  *        Date:    12/13/86
  51.  *
  52.  *   Copyright 1990-1991 North Carolina State University. All Rights Reserved.
  53.  *
  54.  ******************************************************************************
  55.  * $Log: object.c,v $
  56.  * Revision 1.1  1994/08/30  00:04:14  bmott
  57.  * Initial revision
  58.  *
  59.  *****************************************************************************/
  60.  
  61.  
  62. #include <stdio.h>
  63. #include <ctype.h>
  64. #include "asm.h"
  65.  
  66. /* Define the maximum number of bytes (address, data, 
  67.    and checksum) that can be in one S-record */
  68. #define SRECSIZE  36    
  69.  
  70. extern char line[256];
  71. extern FILE *objFile;
  72.  
  73. static char sRecord[80], *objPtr;
  74. static char byteCount, checksum, lineFlag;
  75. static int objAddr;
  76. static char objErrorMsg[] = "Error writing to object file\n";
  77.  
  78. initObj(name)
  79. char *name;
  80. {
  81. short i;
  82.  
  83.     objFile = fopen(name, "w");
  84.     if (!objFile) {    
  85.         puts("Can't open object file");
  86.         exit();
  87.         }
  88.     /* Output S-record file header */
  89. /*    fputs("Here comes an S-record...\n", objFile); */
  90.     fputs("S004000020DB\n", objFile);
  91.     lineFlag = FALSE;
  92. }
  93.  
  94.  
  95. outputObj(newAddr, data, size)
  96. int data, size;
  97. {
  98.     /* If the new data doesn't follow the previous data, or if the S-record
  99.        would be too long, then write out this S-record and start a new one */
  100.     if ((lineFlag && (newAddr != objAddr)) || (byteCount + size > SRECSIZE)) {
  101.         writeObj();
  102.         lineFlag = FALSE;
  103.         }
  104.  
  105.     /* If no S-record is already being assembled, then start making one */
  106.     if (!lineFlag) {
  107.         if ((newAddr & 0xFFFF) == newAddr) {
  108.             sprintf(sRecord, "S1  %04X", newAddr);
  109.             byteCount = 2;
  110.             }
  111.         else if ((newAddr & 0xFFFFFF) == newAddr) {
  112.             sprintf(sRecord, "S2  %06X", newAddr);
  113.             byteCount = 3;
  114.             }
  115.         else {
  116.             sprintf(sRecord, "S3  %08X", newAddr);
  117.             byteCount = 4;
  118.             }
  119.         objPtr = sRecord + 4 + byteCount*2;
  120.         checksum = checkValue(newAddr);
  121.         objAddr = newAddr;
  122.         lineFlag = TRUE;
  123.         }
  124.     
  125.     /* Add the new data to the S-record */
  126.     switch (size) {
  127.         case BYTE : data &= 0xFF;
  128.                 sprintf(objPtr, "%02X", data);
  129.                 byteCount++;
  130.                 checksum += data;
  131.                 break;
  132.         case WORD : data &= 0xFFFF;
  133.                 sprintf(objPtr, "%04X", data); 
  134.                 byteCount += 2;
  135.                 checksum += checkValue(data);
  136.                 break;
  137.         case LONG : sprintf(objPtr, "%08X", data);
  138.                 byteCount += 4;
  139.                 checksum += checkValue(data);
  140.                 break;
  141.         default   : printf("outputObj: INVALID SIZE CODE!\n"); exit();
  142.         }
  143.     objPtr += size*2;
  144.     objAddr += size;
  145. }    
  146.     
  147.  
  148.  
  149. checkValue(data)
  150. int data;
  151. {
  152.     return (data + (data >> 8) + (data >> 16) + (data >> 24)) & 0xFF;
  153. }
  154.  
  155.  
  156. writeObj()
  157. {
  158. char recLen[3];
  159.  
  160.     /* Fill in the record length (including the checksum in the record length */
  161.     sprintf(recLen, "%02X", ++byteCount);
  162.     strncpy(sRecord+2, recLen, 2);
  163.     
  164.     /* Add the checksum (including in the checksum the record length) */
  165.     checksum += byteCount;
  166.     sprintf(objPtr, "%02X\n", (~checksum & 0xFF));
  167.     
  168.     /* Output the S-record to the object file */
  169. /*    fputs("Here comes an S-record...\n", objFile); */
  170.     fputs(sRecord, objFile);
  171.     if (ferror(objFile)) {
  172.         fputs(objErrorMsg, stderr);
  173.         exit();
  174.         }
  175. }
  176.  
  177.  
  178. finishObj()
  179. {
  180.     /* Write out the last real S-record, if present */
  181.     if (lineFlag)
  182.         writeObj();
  183.  
  184.     /* Write out a termination S-record and close the file*/
  185.     fputs("S9030000FC\n", objFile);
  186.     if (ferror(objFile)) {
  187.         fputs(objErrorMsg, stderr);
  188.         exit();
  189.         }
  190.     fclose(objFile);
  191. }    
  192.